package com.ruoyi.business.util;

import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.Func;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Slf4j
public class CheckAccountNameUtil {

    /**
     * 校验姓名
     * @param basisName
     * @param accountName
     * @return
     */
    public static boolean verifyName(String basisName, String accountName) {
        //去除特殊字符
        basisName = removeSpecialStr(basisName.toUpperCase()).
                replaceAll("\\. ", " ").
                replaceAll("\\.", " ").
                replaceAll("-"," ");
        String[] basisNames = Arrays.stream(basisName.split(" ")).filter(p -> StringUtils.isNotBlank(p)).toArray(String[]::new);

        String[] accountNames = accountName.toUpperCase().
                replaceAll("\\. ", " ").
                replaceAll("-"," ").
                replaceAll("\\.", " ").split(",");
        for (int i = 0; i < accountNames.length; i++) {
            //去除特殊字符
            List<String> accountTemps = Arrays.stream(removeSpecialStr(accountNames[i]).split(" ")).filter(p -> StringUtils.isNotBlank(p)).collect(Collectors.toList());
            //匹配
            int matches = 0;
            for (int j = 0; j < basisNames.length; j++) {
                if (matches >= 2) break;
                Iterator<String> iterator = accountTemps.iterator();
                while (iterator.hasNext()) {
                    if (basisNames[j].equals(iterator.next())) {
                        matches++;
                        iterator.remove();
                        break;
                    }
                }
            }
            if (matches >= 2) return true;
        }
        log.info("basisName:" + basisName + ";accountName:" + accountName);
        return false;
    }

    public static void main(String[] args) {
        String s = removeSpecialStr("DAVID-GOKE OBOZUWA").
                replaceAll("\\. ", " ").
                replaceAll("\\.", " ").
                replaceAll("-", " ");
        System.err.println(s);
    }

    /**
     * 去掉 .(点)、空格 -之外的特殊字符
     *
     * @param str
     * @return
     */
    public static String removeSpecialStr(String str) {
        try {
            String regEx = "[\n`~!@#$%^&*()+=|{}':;,'\\[\\]<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(str);
            return m.replaceAll("").trim();
        } catch (Exception e) {
            log.error("removeSpecialStr error", e);
            return str;
        }
    }

    public static String commaConvertSpace(String str) {
        return Func.isEmpty(str) ? str : str.replaceAll(",", " ");
    }

}
